package tech.darkblue.rpcui.util;

import org.apache.commons.lang3.StringUtils;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.dnd.Clipboard;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.TreeItem;

public abstract class SwtUtils {

    public static final int DIALOG_RESIZE = SWT.MAX | SWT.RESIZE;

    public static final int DIALOG_DEFAULT_STYLE = SWT.DIALOG_TRIM | SWT.PRIMARY_MODAL;

    public static void centerDialog(Shell shell, Shell parent) {
        shell.setLocation(parent.getBounds().x + (parent.getBounds().width - shell.getBounds().width)/2,
                parent.getBounds().y + (parent.getBounds().height - shell.getBounds().height)/2);
    }

    public static void expandTreeItems(TreeItem[] treeItems, boolean expanded) {
        for (TreeItem treeItem : treeItems) {
            treeItem.setExpanded(expanded);
        }
    }

    public static void expandAllTreeItems(TreeItem[] treeItems, boolean expanded) {
        for (TreeItem treeItem : treeItems) {
            treeItem.setExpanded(expanded);
            expandAllTreeItems(treeItem.getItems(), expanded);
        }
    }

    public static void addRequiredInputListener(Control[] controls, Control[] requiredControls) {
        for (Control control : controls) {
            if (control instanceof Text) {
                Text text = (Text) control;
                text.addModifyListener(new ModifyListener() {
                    @Override
                    public void modifyText(ModifyEvent arg0) {
                        boolean enabled = true;
                        for (Control controlTemp : controls) {
                            if (controlTemp instanceof Text) {
                                Text textTemp = (Text) controlTemp;
                                if (StringUtils.isEmpty(textTemp.getText())) {
                                    enabled = false;
                                    break;
                                }
                            }
                        }
                        // set status
                        for (Control requiredControl : requiredControls) {
                            requiredControl.setEnabled(enabled);
                        }
                    }
                });
            } else if (control instanceof Group) {
                addRequiredInputListener(((Group) control).getChildren(), requiredControls);
            } else if (control instanceof Composite) {
                addRequiredInputListener(((Composite) control).getChildren(), requiredControls);
            }
        }
    }

    public static void bindTextShortcutKey(KeyEvent event, Text text) {
        if (event.stateMask == SWT.CTRL && event.keyCode == 'a') {
            text.selectAll();
        } else if (event.stateMask == SWT.CTRL && event.keyCode == 'c') {
            Clipboard clipboard = new Clipboard(Display.getDefault());
            clipboard.setContents(new String[] { text.getSelectionText() }, new Transfer[] { TextTransfer.getInstance() });
        }
    }

    public static void bindTextShortcutKey(KeyEvent event, StyledText text) {
        if (event.stateMask == SWT.CTRL && event.keyCode == 'a') {
            text.selectAll();
        } else if (event.stateMask == SWT.CTRL && event.keyCode == 'c') {
            Clipboard clipboard = new Clipboard(Display.getDefault());
            clipboard.setContents(new String[] { text.getSelectionText() }, new Transfer[] { TextTransfer.getInstance() });
        }
    }

}
